package com.dk.controller;

import com.dk.pojo.TbOrder;
import com.dk.result.Result;
import com.dk.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("order")
@RefreshScope      //自动刷新配置, nacos的
@Api
@ApiOperation("订单管理接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询全部
     * @return
     */
    @ApiOperation(value = "查询订单列表")
    @GetMapping("getAll")
     public Result getAll(){
         try {
             List<TbOrder> list = orderService.getAll();
             return Result.OK().setData(list);
         }catch (Exception e) {
             e.printStackTrace();
             return Result.ER();
         }
     }

    /**
     *根据id查询订单信息
     * @param orderId
     * @return
     */
    @ApiOperation(value = "根据id查询订单")
    @ApiImplicitParam(name="orderId",value = "订单主键",required = true, paramType = "path")
    @GetMapping("getById/{orderId}")
     public Result getById(@PathVariable("orderId") Long orderId){
         try {
           TbOrder order =  orderService.getById(orderId);
             return Result.OK().setData(order);
         }catch (Exception e) {
             e.printStackTrace();
             return Result.ER();
         }
     }

    /**
     * 增加订单信息
     * @param order
     * @return
     */
     @PostMapping("addOrder")
     @ApiOperation(value = "增加订单数据")
     @ApiImplicitParam(name="order",value = "订单实体数据",required = true, paramType = "body")
     public Result addOrder(@RequestBody TbOrder order){
         try {
             orderService.addOrder(order);
             return Result.OK();
         }catch (Exception e) {
             e.printStackTrace();
             return Result.ER();
         }
     }

    /**
     * 删除订单信息
     * @param orderId
     * @return
     */
     @DeleteMapping("/{orderId}")
     @ApiOperation(value = "根据订单id删除订单")
     @ApiImplicitParam(name="orderId",value = "订单主键",required = true, paramType = "path")
    public Result deleteOrder(@PathVariable("orderId") Long orderId){
         try {
             orderService.deleteOrder(orderId);
             return Result.OK();
         }catch (Exception e){
             e.printStackTrace();
             return Result.ER();
         }
     }

    /**
     * 修改订单信息
     * @param order
     * @return
     */
    @PutMapping("updateOrder")
    @ApiOperation(value = "修改订单数据")
    @ApiImplicitParam(name="order",value = "订单实体数据",required = true, paramType = "body")
    public Result updateOrder(@RequestBody TbOrder order){
         try {
             orderService.updateOrder(order);
            return Result.OK();
         }catch (Exception e) {
             e.printStackTrace();
             return  Result.ER();
         }
    }
}
